"""
组织机构接口

"""
import os

from flask import Blueprint, request
from werkzeug.utils import secure_filename

from controller.org import get_org_info, update_org_info, update_logo_for_org
from utils.permission import authorize
from utils.views import resp_with_data
from components.base_log import log_method_call

org_bp = Blueprint('org', __name__)


@org_bp.route('/get_my_org', methods=['GET'])
@authorize("system:*:*")
@log_method_call(action='Retrieve')
def get_my_org():
    data = get_org_info()
    return resp_with_data(10000, data=data)


@org_bp.route('/update_my_org', methods=['POST'])
@authorize("system:org:update")
@log_method_call(action='Update')
def update_my_org():
    data = update_org_info()
    return resp_with_data(10000, data=data)


@org_bp.route('/update_org_logo', methods=['POST'])
@authorize("system:*:*")
@log_method_call(action='Update')
def update_org_logo():
    file = request.files['file']
    if file:
        # 文件的保存路径
        filepath = os.path.join('/upload/', secure_filename(file.filename))
        # 保存文件到服务器
        file.save(filepath)
        update_logo_for_org(filepath)
        return resp_with_data(10000)
